<?php 

namespace app\api\controller;

use think\Controller;
use think\Request;
use think\Cache;
use app\common\library\Lottery;


/**
 * 游戏接口
 */ 
class GameController extends BaseController
{
    protected function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 游戏列表
     * @return mixed
     */
    public function index()
    {
        return model('game')->getList();
    }
    /**
     * 游戏入口
     */
    public function read($ename)
    {
        $game = model('game')->getRow(['game_ename' => $ename], 'id, cate_id, game_name, game_ename, game_logo, play_url, brief');
        if (! empty($code)) {
            $game['play_url'] .= '?code=' . $code;
        }

        return $game;
    }

	public function __call($func,$args){
        return ['code'=>'','msg'=>'请检查游戏id参数'];
    }
    /**
     * 进入游戏时的初始化数据入口
     */
    public function getGameStatus()
    {
        $dataPost = json_decode(file_get_contents('php://input'), true);
        if (!isset($dataPost['game_id'])) return ['code' => 0, 'msg' => '游戏id错误'];
        $game_id = $dataPost['game_id'];
        if (!isset($dataPost['user_id'])) return ['code' => 0, 'msg' => '用户id错误'];
        $user_id = $dataPost['user_id'];
        $game_ename = model('common/game')->getValue("id=$game_id", 'game_ename');
        $func = $game_ename . 'Status';
        $re = $this->$func($user_id);
        return $re;
    }
    /**
     * 豪车漂移的初始化数据
     * @param $user_id
     * @return array
     */
    private function hcpyStatus($user_id)
    {
        $bet_list = Cache::get('hcpy_bet_list');
        $re = model('common/game')->getHcpyStatus($user_id, $bet_list);
        $re['data']['game_id'] = 1;
        return $re;
    }
    /**
     * 水果机的初始化数据
     * @param $user_id
     * @return array
     */
    private function sgjStatus($user_id)
    {
        $re = model('common/game')->getSgjStatus($user_id);
        $re['data']['game_id'] = 2;
        return $re;
    }
    /**
     * 飞禽走兽的初始化数据
     * @param $user_id
     * @return array
     */
    private function fqzsStatus($user_id)
    {
        $re = model('common/game')->getFqzsStatus($user_id);
        $re['data']['game_id'] = 3;
        return $re;
    }
    /**
     * 下注接口
     */
    public function setBet(){//user_id game_id bet_id num
        $dataPost = json_decode(file_get_contents('php://input'), true);
        if(!isset($dataPost['game_id'])) return ['code'=>0,'msg'=>'游戏id错误'];
        $game_id=$dataPost['game_id'];
        if((!isset($dataPost['bet_id'])) || $dataPost['bet_id'] > 8) return ['code'=>0,'msg'=>'下注id错误'];
        $bet_id=$dataPost['bet_id'];
        if(!isset($dataPost['user_id'])) return ['code'=>0,'msg'=>'用户id错误'];
        $user_id=$dataPost['user_id'];
        $user_gold=model('common/user')->getValue("id=$user_id",'coin');
        if(!isset($dataPost['num'])) return ['code'=>0,'msg'=>'下注数量错误'];
        $num=$dataPost['num'];
        $game_ename=model('common/game')->getValue("id=$game_id",'game_ename');
        $cache_key="{$game_ename}_bet_list";
        $old_bet_list=Cache::get($cache_key);
        if(!$old_bet_list) $old_bet_list=[];
        if(isset($old_bet_list[$bet_id][$user_id])){
            $old_bet_list[$bet_id][$user_id]+=$num;
            //计算总下注数量
            $sum=0;
            foreach($old_bet_list as $k=>$v){
                if(isset($v[$user_id])){
                    $sum+=$v[$user_id];
                }
            }
        }else{
            $old_bet_list[$bet_id][$user_id]=$num;
        }
        Cache::set($cache_key,$old_bet_list);
        $sum=0;
        foreach($old_bet_list as $k=>$v){
            foreach ($v as $ko=>$vo){
                if($ko == $user_id){
                    $sum+=$vo;
                }
            }
        }
        //不做数据库处理，开奖时处理数据库，环节数据库压力

        $dataPost['sum_num']=$sum;
        $data=$dataPost;
        $data['user_gold']=$user_gold-$sum;
        unset($data['operation']);
        $return_data=[
            'code'=>1,
            'operation'=>$dataPost['operation'],
            'data'=>$data
        ];
        return $return_data;
    }
    /**
     * 游戏开奖入口
     */
    public function getWinResult(){
        $dataPost = json_decode(file_get_contents('php://input'), true);
        //测试用
//        $dataPost= ['operation'=>'getWinResult','game_id'=>2,'user_id'=>1,'type'=>1,'info'=>['num'=>100,'num_get'=>100,'id'=>1]];
//        dump(json_encode($dataPost));die;
//        if(!isset($dataPost['game_id']) ||
//            !in_array($dataPost['game_id'],[1,2,3,4,5,6,7,8,9])) {
//            return ['code'=>0,'msg'=>'游戏id错误'];
//        }
        $game_id=$dataPost['game_id'];
        $game_id=1;
        $game_ename=model('common/game')->getValue("id=$game_id",'game_ename');
        $func=$game_ename.'Result';
        if(isset($dataPost['type'])){
            $func=$func.'_1';
        }
        return $this->$func($dataPost);
    }

    /**
     * 豪车漂移开奖
     */
    public function hcpyResult(){
        //获取游戏相关的下注缓存键，配置，模型方法名
        $cache_key="hcpy_bet_list";
        $config=config("hcpy_option");
        $bet_list=Cache::get($cache_key);
        Cache::rm($cache_key);
        //清理缓存
        $cache_bet=[];
        foreach(array_keys($config) as $v){
            $cache_bet[$v]=[];
        }
        Cache::set($cache_key,$cache_bet);
        $result = model('common/game')->chooseWin_hcpy($bet_list);
        return $result;
    }
    /**
     * 水果机开奖
     */
    public function sgjResult($data=[])
    {
        if (!isset($data['user_id'])) return ['code' => 0, 'msg' => '用户id错误'];
        $result = model('common/game')->chooseWin_sgj($data);
        //将未得分的金币存入缓存
        if ($result['data']['self_result'] > 0) {
            Cache::set('sgj_user_gold' . $result['data']['user_id'], $result['data']['self_result']);
        }
        return $result;
    }
    /**
     * 水果机猜大小
     */
    private function sgjResult_1($data)
    {
        if (!isset($data['user_id'])) return ['code' => 0, 'msg' => '用户id错误'];
        $result = model('common/game')->chooseWin_sgj_1($data);
        //将未得分的金币存入缓存
        if ($result['data']['self_result'] > 0) {
            Cache::set('sgj_user_gold' . $result['data']['user_id'], $result['data']['self_result']);
        }
        return $result;
    }
    /**
     * 水果机点击得分操作
     */
    private function sgjResult_2($data)
    {
//        $data=['game_id'=>'2_2' , 'uname'=>'tzk' ,'num'=>100];
        $gold = model('common/game')->addCoin($data['user_id'], $data['num']);
        if ($gold) {
            return [
                'code' => 1,
                'operation' => 'getWinResult',
                'data' => [
                    'user_gold' => $gold['oGold'],
                    'game_id' => 2,
                    'type' => 2
                ]
            ];
        } else {
            return ['code' => 0, 'msg' => 'error'];
        }
    }

    /**
     *  水果机关闭时自动得分
     */
    public function sgjClose()
    {
        $dataPost = json_decode(file_get_contents('php://input'), true);
        $user_id = $dataPost['user_id'];
        $gold = Cache::get('sgj_user_gold' . $user_id);
        Cache::rm('sgj_user_gold' . $user_id);
        $re = $this->sgjResult_2(['user_id' => $user_id, 'num' => $gold]);
        return $re;
    }
    /**
     * 飞禽走兽开奖
     */
    private function fqzsResult($data){
//        $data=['game_type'=>'3' , 'uname'=>'tzk' ,
//            'info'=>[[1=>100], [2=>200]],
//            'type_info'=>[1=>100,2=>200]];

        //判断开的奖项
        $result=model('common/game')->chooseWin_fqzs($data,$re,$fqzs_option_odds);
        return $result;
    }
}
